package com.zbf.xpath;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class TestXpath {

    public static void main(String[] args) throws IOException {

        ScheduledExecutorService scheduledExecutorService=Executors.newScheduledThreadPool(1);

        String nichengXpath="//*[@id=\"root\"]/div[1]/div[2]/div/div[2]/div/div[1]/div[2]/a/div/span/span/span/span/span/text()";
        String duyinHaoXpath="//*[@id=\"root\"]/div[1]/div[2]/div/div[2]/div/div[1]/div[2]/a/div/span/span/span/span/span";

        String path="//*[@id=\"root\"]/div[1]/div[2]/div/div[2]/div/div[1]/div[1]/a/div/img/@src";

        String title="/html/head/title";

        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("=============开始抓取");
                try{
                    Document document = Jsoup.connect("https://www.douyin.com/video/7026197261109431567").post();
                    JXDocument jxDocument=JXDocument.create(document);
                    Element title1 = document.getElementsByTag("title").first();
                    System.out.println(title1.ownText());
                    log.debug("视频URL={}解析到的标题信息={}","=====url",title1.ownText());
                    //Element body = document.body();
                   /* JXDocument jxDocument=JXDocument.create(document);
                    Element body = document.body();
                    System.out.println("======>"+jxDocument.selNOne(path).toString());*/

                    /*Element element = body.getElementById("root")
                            .child(0)
                            .child(1)
                            .child(0)
                            .child(1)
                            .child(0)
                            .child(0)
                            .child(1)
                            .child(0)
                            .child(0)
                            .child(0).child(0);
                    System.out.println(element.toString().replace("<span>","").replace("</span>",""));*/

                    /*Element elementTitle = body.getElementById("root")
                            .child(0).child(1).child(0).child(0).child(0).child(1).child(0).child(0).child(1);

                    String replace = elementTitle.toString().replace("<span", "").replace("</span>", "").replace(">", "");
                    String replaceAll = replace.replace("-scss\"", "");
                    String replaceAll1 = replaceAll.replace("class=\"_", "");
                    String replaceAll2 = replaceAll1.replace("class=\"", "");
                    Pattern p = Pattern.compile("[a-z0-9 ]");
                    Matcher matcher = p.matcher(replaceAll2);
                    String result = matcher.replaceAll("");
                    System.out.println(result);*/

                    /*Element elementTitle = body.getElementById("root")
                            .child(0).child(1).child(0).child(0).child(0).child(1).child(0).child(1)
                            .child(1).child(1);
                    System.out.println(elementTitle.ownText().replace("发布时间：",""));*/

                    /*Elements aElements = body.getElementsByTag("a");
                    for (Element aElement : aElements) {
                        if (aElement.attr("href").contains("/user/")) {
                            String href1 = aElement.attr("href");
                            if(!href1.contains("https")){
                                href1="https:"+href1;
                            }
                            //解析下一层网页
                            Document documentNext = Jsoup.connect(href1).post();
                            //获取所有的P标签
                            Element bodyNext = documentNext.body();
                            Elements  pTags= bodyNext.getElementsByTag("p");
                            for (Element pTag : pTags) {
                                if(pTag.ownText().contains("抖音号：")){
                                    System.out.println("=====抖音号码===》"+pTag.ownText().replace("抖音号： ",""));
                                    break;
                                }
                            }
                            break;
                        }
                    }*/
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        },0,5, TimeUnit.SECONDS);



    }

}
